package thread.threadlocal;

/**
 * ThreadLocal 基本使用
 *
 * @author liweimin
 * @date 2021/4/30 17:31
 * @since
 */
public class Test01 {
    public static void main(String[] args) {
        SubThread subThread1 = new SubThread();
        SubThread subThread2 = new SubThread();

        subThread1.start();
        subThread2.start();
    }

    static ThreadLocal threadLocal = new ThreadLocal();

    static class SubThread extends Thread {
        @Override
        public void run() {

            for (int i = 0; i < 20; i++) {
                //设置线程关联的值
                threadLocal.set(Thread.currentThread().getName() + "-" + i);
                System.out.println(Thread.currentThread().getName() + "threadLocal 的值为 " + threadLocal.get());
            }
        }
    }
}
